package com.thread.www.demo;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangyang on 2017/10/24.
 */
public class IntLock implements Runnable{
    public static ReentrantLock lock1 = new ReentrantLock();
    public static ReentrantLock lock2 = new ReentrantLock();
    int lock;
    public IntLock(int lock){
        this.lock = lock;
    }

    @Override
    public void run(){
        try {
            if(lock==1){
                lock1.lockInterruptibly();
                Thread.sleep(1000);
                lock2.lockInterruptibly();
                System.out.println("1 : 执行完毕");
            }else{
                lock2.lockInterruptibly();
                Thread.sleep(1000);
                lock1.lockInterruptibly();
                System.out.println("2 : 执行完毕");
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(lock1.isHeldByCurrentThread()){
                lock1.unlock();
            }
            if(lock2.isHeldByCurrentThread()){
                lock2.unlock();
            }
            System.out.println(Thread.currentThread().getName() +  ":线程退出");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        IntLock lock1 = new IntLock(1);
        IntLock lock2 = new IntLock(2);
        Thread t1 = new Thread(lock1);
        Thread t2 = new Thread(lock2);
        t1.start();t2.start();
        Thread.sleep(1000);
        t2.interrupt();
    }
}
